﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataAccess;


namespace iEN.Immediate
{
    public partial class ShowNow : System.Web.UI.Page
    {

        DataTable dtTemp;


        string strRole = "";

        #region protected void Page_Load(object sender, EventArgs e)        //頁面初始判斷有無權限
        protected void Page_Load(object sender, EventArgs e)
        {
            strRole = Session["SysAdmin"].ToString();
            if (!Page.IsPostBack)
            {
                if (Session["SysAdmin"] != null)
                {

                }
                else
                {
                    Response.Write("<script>alert('請先登入！！');location.href='../Login.aspx';</script>");
                }


                try
                {
                    //建立組織群組樹狀圖
                    CreateMeterGroupTree();

                    ParseNode(tvMeterGrp.Nodes[0]);

                }
                catch
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('資料庫抓取資料有誤，無法建立組織群組樹狀圖！')", true);
                    return;
                }


            }

            if (Session["TreeSelected"] != null)
            {

                 ColorNode(tvMeterGrp);
            }

        }
        #endregion

        private void ParseNode(TreeNode node)
        {
            //TODO: work for each node goes here for BF
            if (Session["TreeSelected"] != null && node.Value == Session["TreeSelected"].ToString())
            {
                node.SelectAction = TreeNodeSelectAction.None;
            }

            foreach (TreeNode child in node.ChildNodes)
                ParseNode(child);
        }

        private void ColorNode(TreeView treeView)
        {
            // Print each node recursively.
            TreeNodeCollection nodes = treeView.Nodes;
            foreach (TreeNode n in nodes)
            {
                CallRecursive(n);
            }
        }

        private void CallRecursive(TreeNode treeNode)
        {
            if (treeNode.Value==Session["TreeSelected"].ToString())
            {
                treeNode.Selected=true;
            }
            TreeNodeCollection nodes = treeNode.ChildNodes;
            foreach (TreeNode tn in nodes)
            {
                CallRecursive(tn);
            }

        }



        #region private void CreateMeterGroupTree() //建立樹狀節點
        private void CreateMeterGroupTree()
        {
            //先清空原來的Tree node
            tvMeterGrp.Nodes.Clear();

            //加入根節點
            TreeNode RootNode = new TreeNode();
            RootNode.SelectAction = TreeNodeSelectAction.Select;  //群組層級
            RootNode.Text = "成大宿舍iEN";  //設定節點的顯示文字
            RootNode.Value = "-1";  //設定節點的數值

            DataTable dtOrgGroup = new DBAccess().getOrgGroupTable("1");  //取得ALLOrgGroup
            if (dtOrgGroup == null)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('資料庫抓取資料有誤！')", true);
                return;
            }

            DataTable dtMeter = dtOrgGroup.Copy();
            DataTable dtBuilding = dtOrgGroup.Copy();
            DataTable dtTemp = dtOrgGroup.Copy();
            DataTable dtFloor, dtBlock;
            dtBuilding.DefaultView.RowFilter = "_level=1";
            dtBuilding = dtBuilding.DefaultView.ToTable();

            for (int i = 0; i < dtBuilding.Rows.Count; i++)
            {
                TreeNode tnNode1 = new TreeNode();
                tnNode1.SelectAction = TreeNodeSelectAction.Select;  //群組層級
                tnNode1.Text = dtBuilding.Rows[i]["_name"].ToString();  //設定節點的顯示文字
                tnNode1.Value = dtBuilding.Rows[i]["_id"].ToString(); ;  //設定節點的數值
                RootNode.ChildNodes.Add(tnNode1);
                dtMeter.DefaultView.RowFilter = "_parent_id=" + dtBuilding.Rows[i]["_id"].ToString();
                dtMeter.DefaultView.Sort = " _name";
                dtFloor = dtMeter.DefaultView.ToTable();
                if (dtFloor != null)
                {
                    for (int j = 0; j < dtFloor.Rows.Count; j++)
                    {
                        TreeNode tnNode2 = new TreeNode();
                        tnNode2.SelectAction = TreeNodeSelectAction.Select;  //群組層級
                        tnNode2.Text = dtFloor.Rows[j]["_name"].ToString();  //設定節點的顯示文字
                        tnNode2.Value = dtFloor.Rows[j]["_id"].ToString(); ;  //設定節點的數值
                        tnNode1.ChildNodes.Add(tnNode2);
                        dtTemp.DefaultView.RowFilter = "_parent_id=" + dtFloor.Rows[j]["_id"].ToString();
                        dtTemp.DefaultView.Sort = " _name";
                        dtBlock = dtTemp.DefaultView.ToTable();
                        if (dtBlock != null)
                        {
                            for (int k = 0; k < dtBlock.Rows.Count; k++)
                            {
                                TreeNode tnNode3 = new TreeNode();
                                tnNode3.SelectAction = TreeNodeSelectAction.Select;  //群組層級
                                tnNode3.Text = dtBlock.Rows[k]["_name"].ToString();  //設定節點的顯示文字
                                tnNode3.Value = dtBlock.Rows[k]["_id"].ToString(); ;  //設定節點的數值
                                tnNode2.ChildNodes.Add(tnNode3);
                            }
                        }
                    }
                }
            }
            tvMeterGrp.Nodes.Add(RootNode);
            tvMeterGrp.ExpandAll();
            //tvMeterGrp.CollapseAll();
        }
        #endregion

        protected void tvMeterGrp_SelectedNodeChanged(object sender, EventArgs e)
        {
            int intDepth = tvMeterGrp.SelectedNode.Depth;
            string strID = tvMeterGrp.SelectedNode.Value;

            

            Session["TreeSelected"] = strID;

            switch (intDepth)
            {
                case 0:
                    Response.Redirect("ShowNow.aspx?Page=ShowNowSys.aspx&ID=" + strID);
                    break;
                case 1:
                    Response.Redirect("ShowNow.aspx?Page=ShowNowBuilding.aspx&ID=" + strID);
                    break;
                case 2:
                    Response.Redirect("ShowNow.aspx?Page=ShowNowLevel.aspx&ID=" + strID);
                    break;
                case 3:
                    Response.Redirect("ShowNow.aspx?Page=ShowNowRoom.aspx&ID=" + strID);   
                    break;

            }
               
            

        }




       


    }
}
